package com.wpq.game_company.controller;

import com.wpq.game_company.domain.User;
import com.wpq.game_company.service.IUserService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;



import javax.servlet.http.HttpSession;


@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    //进入登录界面
    @RequestMapping("/login")
    public String login(){
        return "login";
    }

    //登录界面中处理登录请求
    @RequestMapping("/dologin")
    public String dologin(String username,String password,HttpSession session) {

        User finduser = userService.login(username, password);
        if (finduser.getUsername() == null) {
            return "redirect:/user/login";
        }
        if (finduser.getUsername().equals(username) && password.equals(finduser.getPassword())) {
            //把user对象传进去，如果有就代表登录了
            session.setAttribute("username", finduser);
            return "redirect:/user/index";
        }
        //失败
        else {
            //根据查询结果相应的返回，return默认请求下是转发
            return "redirect:/user/login";
        }
    }
    //进入主界面
    @RequestMapping("/index")
    public String index(HttpSession session){
        Object username = session.getAttribute("username");
        if (username == null) {
            return "redirect:/user/login";
        }
        return "index";
    }



    //进入注册界面
    @RequestMapping("/register")
    public String register(){
        return "register";
    }

    @RequestMapping("/doregister")
    public String doregister(User user){

        userService.reg(user);
        return "redirect:/user/login";
    }

    //商务合作
    @RequestMapping("/ProcessCooperation")
    public String P_C(){
        return "ProcessCooperation";
    }

    //意见
    @RequestMapping("/Selfhelpsystem")
    public String SHS(){
        return "Selfhelpsystem";
    }

    //注销账户
    @RequestMapping("/removeuser")
    public String removeuser(HttpSession session){
        User finduser = (User) session.getAttribute("username");

        userService.remove(finduser.getUsername());

        return "redirect:/user/login";
    }




}
